package com.topdt.jbpm.web.servlet.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.thx.common.util.AppContext;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.Todo;
import com.topdt.jbpm.model.WorkAttach;
import com.topdt.jbpm.web.SessionUserUtil;
import com.topdt.jbpm.web.servlet.JbpmServlet;

public class AttachmentAction extends JbpmBaseAction {

  public AttachmentAction(JbpmServlet servlet) {
    super(servlet);
  }

  private List<WorkAttach> workAttachList;

  private String jsonStr;
	/**
	 * 保存上传的附件
	 * 建立日期 : June 25, 2013 6:30:22 PM<br>
	 * 作者 : wangamin<br>
	 * 修改： sunyaofei 2013-08-11
	 * @return
	 */
	public void saveUploadFile(HttpServletRequest request, HttpServletResponse response){
		logger.debug("<进入>:saveUploadFile方法  保存上传的附件");
		String comId = AppContext.getCurrentComId();
		User user  = SessionUserUtil.getCurrentUser();
		
		String workId = request.getParameter(Constants.WORKID_KEY);
		String todoId = request.getParameter(Constants.WORK_TODOID_KEY);
		String[] files = request.getParameterValues("fileId");

		try {
		    for(String fileId:files){
		      Todo todo = new Todo();
              if(StringUtils.isBlank(todoId)){
                  if(StringUtils.isNotBlank(workId)){
                      todo = jbpmService.findTodoByWorkAndUser(workId, user.getId());
                  }
              }else{
                  todo = jbpmService.getTodoById(todoId);
              }

              WorkAttach workAttach = new WorkAttach();
              workAttach.setActivityName(todo.getActivityName());
              workAttach.setHandlerName(todo.getToUserId());
              workAttach.setCompanyId(comId);
              workAttach.setTodoId(todo.getTodoId());
              workAttach.setWorkId(workId);
              workAttach.setFileId(fileId);
              
              attachmentService.saveOrUpdate(workAttach);
              
              responseJson(response, "附件上传成功");
		    }
		} catch (Exception e) {
			logger.error("<错误>:保存上传附件失败",e);
		}

	}
	/**
	 * 跳转到添加附件页面
	 * 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * @return
	 */
	public void show(HttpServletRequest request, HttpServletResponse response){
		logger.debug("<进入>:show方法   跳转到添加附件页面");
		String workId = request.getParameter(Constants.WORKID_KEY);
		try {
      List<WorkAttach> workAttachs = attachmentService.getWorkAttachByWorkId(workId);
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       List<WorkAttach> workAttachList = new ArrayList<WorkAttach>();
       if(workAttachs!=null){
      	 for(WorkAttach work:workAttachs){
      		String uploadTime = sdf.format(work.getSysFile().getUploadTime());
      		work.setUploadTime(uploadTime);
      		workAttachList.add(work);
      	 }
       }
       jsonStr = com.alibaba.fastjson.JSON.toJSONString(workAttachList);
       responseJson(response, jsonStr);
      } catch (Exception e) {
        logger.error("<错误>:附件加载失败",e);
      }
	}

	/**
	 * 删除附件
	 * 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * @return
	 */
    public void deleteAttach(HttpServletRequest request, HttpServletResponse response) {
      logger.debug("<进入>:deleteAttach方法   删除附件");
      try {
        String attachId = request.getParameter("attachId");
        String fileId = request.getParameter("fileId");
        WorkAttach attach = null;
        // 删除物理文件
        if (StringUtils.isNotBlank(attachId)) {
          attach = attachmentService.getAttachById(attachId);
        } else if (StringUtils.isNotBlank(fileId)) {
          attach = attachmentService.getWorkAttachByFileId(fileId);
        }
        if (attach != null) {
          // 删除数据库文件
          attachmentService.deleteAttach(attach);
        }
        responseJson(response, "附件删除成功");
      } catch (Exception e) {
        logger.error("<错误>删除附件", e);
      }
    }

	public List<WorkAttach> getWorkAttachList() {
		return workAttachList;
	}

	public void setWorkAttachList(List<WorkAttach> workAttachList) {
		this.workAttachList = workAttachList;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}
}
